package com.spring.rpc;

import com.spring.rpc.config.RabbitMQClientConfiguration;
import com.spring.rpc.service.IHelloWord;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description
 * @Author cuiyaosheng
 * @Date 2018-04-03 16:14
 * @Modified by:
 **/
public class IHelloInvoker {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(RabbitMQClientConfiguration.class);
        IHelloWord client = context.getBean(IHelloWord.class);

        ExecutorService service = Executors.newFixedThreadPool(1000);
        for (int i = 0; i < 100000; i++) {
            service.execute(new SyncSendWorker(client));
        }
        while(true){
            if(service.isTerminated()){
                System.out.println("任务执行完毕!");
                service.shutdown();
            }else{
                System.out.println("任务继续执行!");
                try {
                    Thread.sleep(100000);
                    continue;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public static class SyncSendWorker implements Runnable{
        private IHelloWord helloWord = null;

        public SyncSendWorker(IHelloWord helloWord ) {
            this.helloWord = helloWord;
        }

        @Override
        public void run() {
            this.helloWord.sayHello("HelloHoHelloHelloHelloHelloHelloHello");
        }
    }
}
